package com.livesource.client.webservices.subversion;

import com.google.gwt.json.client.JSONArray;
import com.google.gwt.json.client.JSONException;
import com.google.gwt.json.client.JSONObject;
import com.livesource.client.model.entry.Field;
import com.livesource.client.model.entry.Method;
import com.livesource.client.model.entry.ProjectClass;
import com.livesource.client.model.entry.ProjectFeature;
import com.livesource.client.utilities.ConvertJsonp;
import com.livesource.client.utilities.FeatureNameGenerator;

public class ConvertJsonpToJavaDocumentation {

	public static ProjectClass convert(ProjectClass projectClass,
			JSONObject entry) {

		projectClass.setFileContent(ConvertJsonp.convertToString(entry
				.get("fileContent")));

		getClass(projectClass, entry);

		JSONArray objConstructors = entry.get("class_constructors").isArray();

		Method[] constructorsArray = new Method[objConstructors.size()];

		for (int i = 0; i < objConstructors.size(); i++) {

			JSONObject methodJsonObj = (JSONObject) objConstructors.get(i);

			constructorsArray[i] = ConvertJsonpToJavaDocumentation
					.getMethod(methodJsonObj);
		}

		projectClass.setConstructors(constructorsArray);

		JSONArray objMethods = entry.get("class_methods").isArray();

		Method[] methodsArray = new Method[objMethods.size()];

		for (int i = 0; i < objMethods.size(); i++) {

			JSONObject methodJsonObj = (JSONObject) objMethods.get(i);

			methodsArray[i] = ConvertJsonpToJavaDocumentation
					.getMethod(methodJsonObj);
		}

		projectClass.setMethods(methodsArray);

		JSONArray objFields = entry.get("class_fields").isArray();

		Field[] fieldsArray = new Field[objFields.size()];

		for (int i = 0; i < objFields.size(); i++) {

			JSONObject fieldJsonObj = (JSONObject) objFields.get(i);

			fieldsArray[i] = ConvertJsonpToJavaDocumentation
					.getField(fieldJsonObj);
		}

		projectClass.setFields(fieldsArray);

		return projectClass;
	}

	private static void getClass(ProjectClass projectClass, JSONObject obj) {

		projectClass.setClassPackage(ConvertJsonp.convertToString(obj
				.get("class_package")));

		projectClass.setClassName(ConvertJsonp.convertToString(obj
				.get("class_name")));

		projectClass.setFeatureDescription(ConvertJsonp.convertToString(obj
				.get("class_description")));

		projectClass.setSuperclass(ConvertJsonp.convertToString(obj
				.get("super_class")));

		projectClass.setModifiers(ConvertJsonp.convertToString(obj
				.get("class_modifiers")));

		if (obj.get("class_tags") != null) {

			projectClass.setStatus(getStatus(obj.get("class_tags").isArray()));

			projectClass.setFeatureType(getClassType(obj.get("class_tags")
					.isArray()));
		}
	}

	public static Method getMethod(JSONObject methodJsonObj) {

		Method method = new Method();

		method.setName(ConvertJsonp.convertToString(methodJsonObj
				.get("method_name")));

		method.setSpacedName(FeatureNameGenerator.spacedName(method.getName()));

		method.setDescription(ConvertJsonp.convertToString(methodJsonObj
				.get("method_description")));

		method.setParameters(ConvertJsonp.convertToString(methodJsonObj
				.get("method_parameters")));

		method.setReturnType(ConvertJsonp.convertToString(methodJsonObj
				.get("method_return_type")));

		method.setModifiers(ConvertJsonp.convertToString(methodJsonObj
				.get("method_modifiers")));

		return method;
	}

	public static Field getField(JSONObject fieldJsonObj) {

		Field field = new Field();

		field.setName(ConvertJsonp.convertToString(fieldJsonObj
				.get("field_name")));

		field.setType(ConvertJsonp.convertToString(fieldJsonObj
				.get("field_type")));

		return field;
	}

	private static String getStatus(JSONArray jsonTags) {

		String tagStatus = null;

		for (int i = 0; i < jsonTags.size(); i++) {

			String tag;
			try {
				tag = jsonTags.get(i).toString().replaceAll("@", "");

				tag = tag.replaceAll("\"", "");

				if (ProjectFeature.TODO_STATUS.equals(tag)
						|| ProjectFeature.INPROGRESS_STATUS.equals(tag)
						|| ProjectFeature.BLOCKED_STATUS.equals(tag)) {

					tagStatus = tag;
				}

			} catch (JSONException e) {

				e.printStackTrace();
			}
		}

		return tagStatus;
	}

	private static String getClassType(JSONArray jsonTags) {

		String tagType = null;

		for (int i = 0; i < jsonTags.size(); i++) {

			try {

				String tag = jsonTags.get(i).toString().replaceAll("@", "");

				tag = tag.replaceAll("\"", "");

				if (ProjectFeature.FEATURE_TYPE.equals(tag)
						|| ProjectFeature.USER_INTERFACE_TYPE.equals(tag)
						|| ProjectFeature.ARCHITECTURE_TYPE.equals(tag)
						|| ProjectFeature.ENTITY_TYPE.equals(tag)) {

					tagType = tag;
				}

			} catch (JSONException e) {

				e.printStackTrace();
			}
		}

		return tagType;
	}
}
